package com.example.reagent_management.service.works;

import android.content.Context;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import androidx.work.Data;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.ExistingWorkPolicy;
import androidx.work.ListenableWorker;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import dagger.hilt.android.qualifiers.ApplicationContext;

/**
 * 一次性 工作
 */
public class WorksBuild {

    private static WorkManager manager;

    @Inject
    public WorksBuild(WorkManager workManager) {
        manager = workManager;
    }

    public Builder Builder() {
        return new Builder();
    }

    public WorkManager getManager() {
        return manager;
    }

    public static class Builder {

        public Builder() {

        }

        private Data mData;
        private OneTimeWorkRequest.Builder builder;
        private PeriodicWorkRequest.Builder builder1; //定期工作  用于提醒或上传日志或

        public Builder setInputData(Data data, Class<? extends ListenableWorker> clazz) {
            this.mData = data;
            //kotlin调用 val historyInsertWork = OneTimeWorkRequestBuilder<HistoryWork>()
            //https://developer.android.google.cn/topic/libraries/architecture/workmanager/how-to/define-work?hl=zh_cn
            // 这里的只用了一次性工作或延迟工作
            builder = new OneTimeWorkRequest.Builder(clazz);
            return this;
        }

        /**
         * @param repeatDay 设置重复时间，最后15分钟才开始执行
         * @param clazz
         * @return
         */
        public Builder setRepeatWorkTime(int repeatDay, Class<? extends ListenableWorker> clazz) {
            builder1 = new PeriodicWorkRequest.Builder(clazz, repeatDay, TimeUnit.DAYS, 15, TimeUnit.MINUTES);
            return this;
        }

        /**
         * 一次性工作
         */
        public void build() {
            manager.enqueue(builder.setInputData(mData).build());
        }

        /**
         * 延迟工作
         * 添加标记和时间
         *
         * @param tag
         * @param time
         */
        public UUID build(String tag, Long time) {
            final OneTimeWorkRequest delayWork = builder.setInputData(mData).setInitialDelay(time, TimeUnit.MILLISECONDS)
                    .addTag(tag).build();
            manager.enqueue(delayWork);
            return delayWork.getId();
        }

        /**
         * 唯一工作队列并返回UUID/定期闹钟清理工作
         */
        public UUID build(int type, String workName) {
            UUID uuid = null;
            if (type == 0) {
                final OneTimeWorkRequest oneWork = builder.setInputData(mData).build();
                //唯一工作链 策略是加入工作队列中
                manager.enqueueUniqueWork(
                        workName,
                        ExistingWorkPolicy.APPEND_OR_REPLACE,
                        oneWork
                );
                uuid = oneWork.getId();
            } else if (type == 1) {
                final PeriodicWorkRequest repeatWork = builder1.build();
                manager.enqueueUniquePeriodicWork(workName, ExistingPeriodicWorkPolicy.KEEP, repeatWork);
                uuid = repeatWork.getId();
            }
            return uuid;
        }
    }
}
